#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#  net library ebooks are reported differently. we capture stats by subject of books downloaded for reading
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Decode_Month Add_Delta_YM Add_Delta_Days Today);

use avsCommon;



dbsetup();

my ($content, $response, $request, $url);
my $session;

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );
$ua->timeout(60);
$ua->env_proxy;

# access vendor site to get logged in
getLogin();

# loop through all the colleges
do {
	getStats ();
} while ($sthlogin->fetch);	

dbshutdown();

doLogout();

exit();

sub getLogin {
	$url='http://extranet.netlibrary.com/resourcecenter/proc_login.asp';

	$request = HTTP::Request->new(POST => $url);
	$request->content_type('application/x-www-form-urlencoded');
	$request->content("SUBMITTEDFORM=true&User_Alias=$vendorusername&User_Password=$vendorpassword");
	$response = $ua->request($request);

	if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
		die "#error:$0: POST report $url : " . $response->status_line;
	};
}

sub getStats {
	# set the correct library
	$url = "http://extranet.netlibrary.com/resourcecenter/set_library.asp?id=$option1";
	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET login $url :" . $response->status_line;
	};

	my ($runyear, $runmonth, $runday) = @begdate;

	# date loop. run once per month requested
	do {
		my @options;
		my @collections;
		my $collegevalue;

		# save content for debugging. store content per college in one file rather than indiviudal files
		savecontent('Netlibrary', "$collegeid-$runyear-$runmonth", '');
		open AFILE, ">>../DataDownloads/Netlibrary/$collegeid-$runyear-$runmonth.txt";

		#post to get the GUID and options
		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_customize.asp';
		$request = HTTP::Request->new(POST => $url);
		$request->content_type('application/x-www-form-urlencoded');
		$request->content('ReportType=U3&Backdoor=');
		$response = $ua->request($request);

		if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
			die "#error:$0: POST SW GUID $url : " . $response->status_line;
		};

		$content = $response->decoded_content;

		# retrieve the GUID and collections information
		$content =~ /NAME='GUID' VALUE='(?<guid>.*?)'.*?'AvailableCollections'.*?>(?<data>.*?)<\/SELECT>/s;
		my $guid = $+{guid};
		my $data = $+{data};

		# save the collections <select><options ...
		while ($data =~ /<OPTION VALUE=\"(?<value>.*?)\">(?<option>.*?)<\/OPTION>/sgc) {
			push @options, $+{option};

			# urlencode, but not completely, change space (%20) back to +
			my $value = urlencode($+{value});
			$value =~ s/%20/+/g;
			push @collections, $value;

			# save the entire option value if the digits at begining are same as our college $option1
			if (substr($value, 0, length($option1)) eq $option1) {
				$collegevalue = $value;
			}
		}

		my $getGuid;
		
		# collection loop, run once per collection then once for ebook, eaudio, ejournal....
		foreach my $collection (@collections) {
			my $option = shift @options;
			
			# loop through ebooks, eaudion, ejournal
			my $statstype = 3;
			foreach my $contentclass ('EBOOK','ABOOK','JNL') {
			
				if ($getGuid) {
					#post to get the GUID and options
					$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_customize.asp';
					$request = HTTP::Request->new(POST => $url);
					$request->content_type('application/x-www-form-urlencoded');
					$request->content('ReportType=U3&Backdoor=');
					$response = $ua->request($request);

					if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
						die "#error:$0: POST SW GUID $url : " . $response->status_line;
					};

					$content = $response->decoded_content;

					$content =~ /NAME='GUID' VALUE='(?<guid>.*?)'/s;
					$guid = $+{guid};
				}

				$getGuid = 1;

				# setup dates to pass in url
				my $period = myperiod($runyear, $runmonth);
				$runmonth = sprintf('%02d', $runmonth);
				my ($runendyear, $runendmonth, $runendday) = 	Add_Delta_Days(Add_Delta_YM($runyear, $runmonth, 1, 0, 1), -1); # find last day of month by going to first day of next month and moving back one day
				$runendmonth = sprintf('%02d', $runendmonth);

				#
				# get the report started based on collection parameters from above
				#

				$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_waiting.asp';
				$request = HTTP::Request->new(POST => $url);
				$request->content_type('application/x-www-form-urlencoded');
	#			$request->content("GUID=$guid&ReportType=U3&Interval=CustomDate&MonthFrom=$runmonth&DayFrom=01&YearFrom=$runyear&MonthTo=$runendmonth&DayTo=$runendday&YearTo=$runendyear&contentClass=EBOOK&PageSize=500&grpCollection=CustomUsage&SelectedCollections=$collection&grpSelectMembers=MemberUsage&SelectedMembers=$collegevalue");
				$request->content("GUID=$guid&ReportType=U3&Interval=CustomDate&MonthFrom=$runmonth&DayFrom=01&YearFrom=$runyear&MonthTo=$runendmonth&DayTo=$runendday&YearTo=$runendyear&contentClass=$contentclass&PageSize=500&grpCollection=CustomUsage&SelectedCollections=$collection&grpSelectMembers=MemberUsage&SelectedMembers=$collegevalue");
				$response = $ua->request($request);

				if (!$response->is_success) {
					die "#error:$0: POST SW report $url : " . $response->status_line;
				};

				# ask for the tab delimited file
				$url = "http://extranet.netlibrary.com/resourcecenter/reports/proc_reports_display.asp?ReportFormat=1&reference_ebooks=0&itcenter_ebooks=0&CreateFrom=$runyear-$runmonth-1&CreateTo=$runendyear-$runendmonth-$runendday&Interval=CUSTOMDATE&Sort=&Direction=ASC&PrevSortDirection=DontSort&ReportType=U3&GUID=$guid&PageSize=500&grpIncludeFree=1&NewBookInterval=30&Scope=CustomUsage&TurnawayType=1&contentClass=$contentclass";
				$response = $ua->request(GET $url);

				# if there is no data request will generate a 500 status error.
				if (substr($response->status_line,0,3) ne '500') {

					# die on other server errors
					if (!$response->is_success) {
						die "#error:$0: GET login $url :" . $response->status_line;
					}

					$content = $response->decoded_content;

					# save content for debugging
					print AFILE "\n#$contentclass-$option\n$content\n";

					$content =~ /Total Accesses:\s(?<accesses>\d*)/;
					my $accesses = $+{accesses};
					$accesses =~ s/,//g;	# remove any commas
					print "$vendorid $collegeid  $period $option $accesses $contentclass\n";
					$sthstats->execute($vendorid, $collegeid, "$option", $period, $accesses, $statstype)
						or die '#error: sthstats execute failed';
				
				}

				$statstype++;

	#			# ask to display the report
	#			$url = 'http://extranet.netlibrary.com/resourcecenter/reports/proc_reports_display.asp';
	#			$request = HTTP::Request->new(POST => $url);
	#			$request->content_type('application/x-www-form-urlencoded');
	#			$request->content("MonthFrom=$runmonth&DayFrom=01&GUID=$guid&YearTo=$runendyear&contentClass=EBOOK&SelectedCollections=$collection&SelectedMembers=$collegevalue&ReportType=U3&Interval=CustomDate&YearFrom=$runyear&grpSelectMembers=MemberUsage&MonthTo=$runendmonth&DayTo=$runendday&PageSize=500&grpCollection=CustomUsage");
	#			$response = $ua->request($request);
	#
	#			if (!$response->is_success) {
	#				die "#error:$0: POST report $url : " . $response->status_line;
	#			};
	#
	#			if (index($content, 'No records were found at this time.') > -1) {
	#				print "#info: No SW records found for $collegeid $runyear $runmonth $option\n";
	#			}
	#
	#			else {
	#				$content =~ /Total Accesses: &nbsp;<B>(?<accesses>.*?)<\/B>/;
	#				my $accesses = $+{accesses};
	#				$accesses =~ s/,//g;	# remove any commas
	#				my $collegeid  = $college{$columns[2]};
	#				print "$vendorid $collegeid  $period $option 0 $accesses\n";
	#				$sthstats->execute($vendorid, $collegeid, "$option", $period, $accesses, 3) if $accesses > 0;
	#			}
	
			}
		}	
		# close debuging file
		close AFILE;

		# advance the month
		($runyear, $runmonth, $runday) = Add_Delta_YM($runyear, $runmonth, $runday, 0, 1);	# advance the month

	} while ($runyear < $endyear || $runyear == $endyear && $runmonth <= $endmonth);

}
#		# have to create a ,+ format from the URL encoded format... sigh
#		my $statewidecommadelim = $statewide;
#		my $localcommadelim = $local;
#		$statewidecommadelim =~ s/%23&SelectedCollections=/%23%2C+/g;
#		$localcommadelim =~ s/%23&SelectedCollections=/%23%2C+/g;
#
#		# setup dates to pass in url
#		my $period = myperiod($runyear, $runmonth);
#		$runmonth = sprintf('%02d', $runmonth);
#		my ($runendyear, $runendmonth, $runendday) = 	Add_Delta_Days(Add_Delta_YM($runyear, $runmonth, 1, 0, 1), -1); # find last day of month by going to first day of next month and moving back one day
#		$runendmonth = sprintf('%02d', $runendmonth);
#		
#		my @contents;
#	
#		#
#		# get the report started based on STATEWIDE parameters from above
#		#
#
#		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_waiting.asp';
#		$request = HTTP::Request->new(POST => $url);
#		$request->content_type('application/x-www-form-urlencoded');
#		$request->content("GUID=$guid&ReportType=U3&Interval=CustomDate&MonthFrom=$runmonth&DayFrom=01&YearFrom=$runyear&MonthTo=$runendmonth&DayTo=$runendday&YearTo=$runendyear&contentClass=EBOOK&PageSize=500&grpCollection=CustomUsage$statewide&grpSelectMembers=MemberUsage&SelectedMembers=$collegevalue");
#		$response = $ua->request($request);
#
#		if (!$response->is_success) {
#			die "#error:$0: POST SW report $url : " . $response->status_line;
#		};
#
#		# ask to display the report
#		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/proc_reports_display.asp';
#		$request = HTTP::Request->new(POST => $url);
#		$request->content_type('application/x-www-form-urlencoded');
#		$request->content("MonthFrom=$runmonth&DayFrom=01&GUID=$guid&YearTo=$runendyear&contentClass=EBOOK$statewidecommadelim&SelectedMembers=$collegevalue&ReportType=U3&Interval=CustomDate&YearFrom=$runyear&grpSelectMembers=MemberUsage&MonthTo=$runendmonth&DayTo=$runendday&PageSize=500&grpCollection=CustomUsage");
#		$response = $ua->request($request);
#
#		if (!$response->is_success) {
#			die "#error:$0: POST report $url : " . $response->status_line;
#		};
#
#		$content = $response->decoded_content;
#
#		savecontent('Netlibrary', "$collegeid-$runyear-$runmonth-sw", $content);
#		
#		if (index($content, 'No records were found at this time.') > -1) {
#			print "#info: No SW records found for $collegeid\n";
#		}
#		
#		else {
#			$content =~ /Content Type(?<data>.*?)<\/TABLE>/s;
#			$data = $+{data};
#			$data =~ s/<.*?>/ /g; # remove html
#			$data =~ s/&nbsp;//g; # remove spaces
#			# only difference is '- SW' in datasource
#			while ($data =~ /\s*(?<datasource>.*?)\s*(?<ereads>\d+)\s*eBook/sgc) {
#				print "$vendorid $collegeid  $period $+{datasource} - SW 0 $+{ereads}\n";
#				$sthestats->execute($vendorid, $collegeid, "$+{datasource} - SW", $period, 0, "0$+{ereads}");
#			}
#		}
#		
#		#
#		# get local databases
#		#
#		
#		#post to get new guid
#		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_customize.asp';
#		$request = HTTP::Request->new(POST => $url);
#		$request->content_type('application/x-www-form-urlencoded');
#		$request->content('ReportType=U3&Backdoor=');
#		$response = $ua->request($request);
#
#		if (!$response->is_success && substr($response->status_line,0,3) ne '302') {
#			die "#error:$0: POST LC GUID $url : " . $response->status_line;
#		};
#		
#		$content = $response->decoded_content;
#
#		$content =~ /NAME='GUID' VALUE='(?<guid>.*?)'/s;
#		$guid = $+{guid};
#
#		# get the report started based on LOCAL parameters from above
#		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/reports_waiting.asp';
#		$request = HTTP::Request->new(POST => $url);
#		$request->content_type('application/x-www-form-urlencoded');
#		$request->content("GUID=$guid&ReportType=U3&Interval=CustomDate&MonthFrom=$runmonth&DayFrom=01&YearFrom=$runyear&MonthTo=$runendmonth&DayTo=$runendday&YearTo=$runendyear&contentClass=EBOOK&PageSize=500&grpCollection=CustomUsage$local&grpSelectMembers=MemberUsage&SelectedMembers=$collegevalue");
#		$response = $ua->request($request);
#
#		if (!$response->is_success) {
#			die "#error:$0: POST LC report $url : " . $response->status_line;
#		};
#
#		# ask to display the report
#		$url = 'http://extranet.netlibrary.com/resourcecenter/reports/proc_reports_display.asp';
#		$request = HTTP::Request->new(POST => $url);
# 		$request->content_type('application/x-www-form-urlencoded');
# 		$request->content("MonthFrom=$runmonth&DayFrom=01&GUID=$guid&YearTo=$runendyear&contentClass=EBOOK$localcommadelim&SelectedMembers=$collegevalue&ReportType=U3&Interval=CustomDate&YearFrom=$runyear&grpSelectMembers=MemberUsage&MonthTo=$runendmonth&DayTo=$runendday&PageSize=500&grpCollection=CustomUsage");
# 		$response = $ua->request($request);
# 
# 		if (!$response->is_success) {
# 			die "#error:$0: POST LC report $url : " . $response->status_line;
# 		};
#
#		$content = $response->decoded_content;
#
#		savecontent('Netlibrary', "$collegeid-$runyear-$runmonth-lc", $content);
#
#		if (index($content, 'No records were found at this time.') > -1) {
#			print "#info: No LC records found for $collegeid\n";
#		}
#		
#		else {
#
#			$content =~ /Content Type(?<data>.*?)<\/TABLE>/s;
#			$data = $+{data};
#			$data =~ s/<.*?>/ /g; # remove html
#			$data =~ s/&nbsp;//g; # remove spaces
#
#			while ($data =~ /\s*(?<datasource>.*?)\s*(?<ereads>\d+)\s*eBook/sgc) {
#				print "$vendorid $collegeid  $period $+{datasource} 0 $+{ereads}\n";
#				$sthestats->execute($vendorid, $collegeid, $+{datasource}, $period, 0, "0$+{ereads}");
#			}
#		}				

sub doLogout {

		$url = 'http://extranet.netlibrary.com/resourcecenter/logout.asp';
		$response = $ua->request(GET $url);

}